package com.itgaohe.controller;

import com.itgaohe.common.context.UserContext;
import com.itgaohe.dao.entity.Blog;
import com.itgaohe.dto.resp.SearchBlogDTO;
import com.itgaohe.service.BlogCommentsService;
import com.itgaohe.service.BlogService;
import com.itgaohe.service.LikeService;
import com.itgaohe.utils.CreateCursorUtil;
import com.itgaohe.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping
@Tag(name = "博客管理", description = "用户相关操作")
public class BlogController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private LikeService likeService;

    @Autowired
//    查询用户本人博客 根据令牌中的个人id
    private BlogCommentsService blogCommentsService;
    @Operation(summary = "获取用户博客")
    @GetMapping("/blog/of/me")
    public Result getMyBlog(Long cursor) {
        Result blogBykeyId = blogService.getBlogBykeyId(cursor);
        return blogBykeyId;
    }

    //        查询博客详情 根据id查
    @Operation(summary = "根据id获取博客详情")
    @GetMapping("/blog/{id}")
    public Result getBlogbyId(@PathVariable long id) {
        SearchBlogDTO blogById = blogService.getBlogById(id);
        return Result.ok(blogById);
    }

    //    根据userid查
    @Operation(summary = "根据用户id查询博客")
    @GetMapping("/blogs/{userId}")
    public Result getBlogbyUserId(@PathVariable long userId,Long cursor) {
        Result blogbyUserId = blogService.getBlogbyUserId(userId, cursor);
        return blogbyUserId;
    }

    //查询所有博客
    @Operation(summary = "查询所有博客")
    @GetMapping("/blog/list")
    public Result getHotBlog(@RequestParam Long cursor) {
        Result blogslist = blogService.getBlogslist(cursor);
        return blogslist;
    }

    //博客点赞
    @Operation(summary = "根据博客id获取博客评论")
    @PutMapping("/blog/like/{blogId}")
    public Result likeBlog(@PathVariable long blogId) {

        blogService.blogAllLike(blogId);
        blogService.likeBlog(blogId);
        return Result.ok();

    }
    //查询博客是否点赞
    @Operation(summary = "根据博客id判断是否点赞")
    @GetMapping("/blog/isLike/{blogId}")
    public Result getIsLike(@PathVariable Long blogId) {
        Boolean isLike = likeService.getIsLike(blogId);

        return Result.ok(isLike);
    }
    /**
     * 创建博客
     */
    @Operation(summary = "创建博客")
    @PostMapping("/blog")
    public Result createBlog(@RequestBody Blog blog) {
        Long userId = UserContext.getUser().getId();
        String title = blog.getContent();
        blog.setTitle(title);
        blog.setUserId(userId);
        blog.setLiked(0);
        blog.setComments(0);
        Long cursor = CreateCursorUtil.getBlogCursor(Long.valueOf(blog.getLiked()),userId);
        blog.setCursorIndex(cursor);
        blogService.save(blog);
        return Result.ok();
    }

}


